# -*- coding: utf-8 -*-
"""
Created on Thu Feb 20 10:52:26 2025

@author: zm-office

# ui文件转py文件
"""

import os
import subprocess

def convert_ui_to_py(ui_dir, py_dir, tool='pyuic5'):
    """
    批量转换 .ui 文件到 .py
    :param ui_dir: .ui 文件目录
    :param py_dir: 输出 .py 文件目录
    :param tool: 转换工具 (pyuic5 或 pyside2-uic)
    """
    if not os.path.exists(py_dir):
        os.makedirs(py_dir)

    for ui_filename in os.listdir(ui_dir):
        print(ui_filename)
        if 'ManagePdf' not in ui_filename:
            continue
        if ui_filename.endswith('.ui'):
            ui_file = os.path.join(ui_dir, ui_filename)
            py_filename = os.path.splitext(ui_filename)[0] + '.py'
            py_file = os.path.join(py_dir, py_filename)
            
            try:
                subprocess.run([tool, ui_file, '-o', py_file], check=True)
                print(f'成功转换: {ui_filename} -> {py_filename}')
            except subprocess.CalledProcessError as e:
                print(f'转换失败: {ui_filename}\n错误信息: {e}')

if __name__ == '__main__':
    # 配置路径
    ui_directory = r'E:\MyShare\codes\python\GuiPy\managePdf\ui'  # .ui 文件所在目录
    py_directory = r'E:\MyShare\codes\python\GuiPy\managePdf\src'  # 输出 .py 文件目录
    
    # 选择转换工具
    conversion_tool = 'pyside6-uic'
    
    convert_ui_to_py(ui_directory, py_directory, conversion_tool)